let lotteryData = [
  {
    name: '爆款0元预约名额',
    main: `<li class="twoList">品牌热门产品</li>
          <li class="twoList">现场低至5折&nbsp 免费预约</li>`
  },
  {
    name: '100元现金红包',
    main: `<li>超级会员福利</li>
          <li>￥<span>100</span></li>
          <li>现场支付满100返还100元现金</li>`
  },
  {
    name: '100元现金红包',
    main: `<li>VIP平台券</li>
          <li>￥<span>100</span></li>
          <li>现场支付满100返还100元现金</li>`
  },
  {
    name: '千元品牌优惠券礼包',
    main: `<li class="twoList">大额商户优惠券</li>
          <li class="twoList">领券下单更划算</li>`
  }
]

window.onload = function () {
  function random() {
    const chars = ['1', '2', '3', '4',];
    let res;
    let id = Math.floor(Math.random() * 10 / 2);
    res = chars[id];
    return typeof res === 'undefined' ? random() : res
  }

  function generate(num) {
    num = num - 1;
    return lotteryResult = `
      <p class="slogan slideInDown">中奖啦！</p>
      <p class="name slideInDown">恭喜获得<span>` + lotteryData[num].name + `</span></p>
      <div class="box flipInX">
        <ul class="">` + lotteryData[num].main + `</ul>
      </div>
      <p class="account">仅限11月2-4日 到华夏家博会现场使用</p>
      <div class="btn pulse" id="getPrize">
      <div class="btn-main">
        <span class="btn-text">获取大奖</span>
        <span class="finger"></span>
      </div>
      </div>
    `;
  }

  addEvent($('draw-btn'), 'click', () => {
    let num = random();
    $('result').innerHTML = generate(num);
    addClass($('pagingOpen'), 'show');
    addClass($('money'), 'bounceInDown');
    addClass($('paginglCose'), 'hide');
    addEvent($('getPrize'), 'click', () => {
      window.location.href = 'https://www.baidu.com'
    })
  });
  addEvent($('closed'), 'click', () => {
    console.log('关闭了')
  })

  //公有方法
  function addEvent(ele, eventName, fun) {
    if (ele.addEventListener) {
      ele.addEventListener(eventName, fun, false);
    } else {
      ele.attachEvent("on" + eventNme, fun);
    }
  }

  function $(str) {
    return document.getElementById(str)
  }

  function hasClass(elem, cls) {
    cls = cls || '';
    if (cls.replace(/\s/g, '').length === 0) return false;
    return new RegExp(' ' + cls + ' ').test(' ' + elem.className + ' ');
  }

  function addClass(elem, cls) {
    if (!hasClass(elem, cls)) {
      elem.className = elem.className === '' ? cls : elem.className + ' ' + cls;
    }
  }

};
